package com.cyou.dream.factory;

import com.cyou.dream.core.IRule;
import com.cyou.dream.define.ERule;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

public class RuleFactory {

    private static final Map<String, Supplier<IRule>> ruleSuppliers = new HashMap<>();

    static {
        Arrays.stream(ERule.values())
                .forEach(rule -> ruleSuppliers.put(rule.name(), rule.getRuleSupplier()));
    }

    public static IRule create(String ruleType) {
        return Optional.ofNullable(ruleSuppliers.get(ruleType))
                .map(Supplier::get)
                .orElseThrow(() -> new IllegalArgumentException("Unknown rule type: " + ruleType));
    }
}